﻿(function ($) {

    $(function () {
        var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
        $.getScript(scriptbase + "SP.js", function () {
            $.getScript(scriptbase + "SP.Taxonomy.js", GetNewComers('#myCarouselNewComers .carousel-inner'));
        });
    });

    /*javascript for New Comer*/
    this.GetNewComers = function (idcontent) {
        makeSlide();
        $('#newcomerloading').show();
        var currentId = 'newcomer';
        var itemrowlimit = getRowLimitFromContentEditorWP(currentId);
        var siteUrl = '/teams/OurVoiceHN/News/';
        var clientContext = new SP.ClientContext(siteUrl);

        var oList = clientContext.get_web().get_lists().getByTitle('Posts');
        var camlQuery = new SP.CamlQuery();
        //camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Is_x0020_Active\' /><Value Type=\'Boolean\'>1</Value></Eq><Eq><FieldRef Name=\'Subject_x0020_Type\' /><Value Type=\'Choice\'>Microsoft People</Value></Eq></And></Where><OrderBy><FieldRef Name=\'Top_x0020_News\' Ascending=\'FALSE\' /><FieldRef Name=\'PublishedDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>' + itemrowlimit + '</RowLimit></View>');
        camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Is_x0020_Active\' /><Value Type=\'Boolean\'>0</Value></Eq><Eq><FieldRef Name=\'Subject_x0020_Type\' /><Value Type=\'Choice\'>Microsoft People</Value></Eq></And></Where><OrderBy><FieldRef Name=\'PublishedDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>' + itemrowlimit + '</RowLimit></View>');
        var items = oList.getItems(camlQuery);
        clientContext.load(items);
        clientContext.executeQueryAsync(
            function () {
                $(idcontent).html('');
                $('#newcomerloading').hide();
                var listEnumerator = items.getEnumerator();
                var index = 0;
                while (listEnumerator.moveNext()) {
                    var item = listEnumerator.get_current();
                    var url = siteUrl + 'Lists/Posts/Post.aspx?ID=' + item.get_item('ID');

                    var urlLink = item.get_item("Url_x0020_Link");
                    if (urlLink !== null && $.trim(urlLink) !== '') {
                        url = urlLink.get_url();
                    }

                    var newComer = BuildNewCommers(url, item.get_item('Job_x0020_Title'), item.get_item('Full_x0020_Name'), item.get_item('Department'), item.get_item('PublishingPageIcon'), index == 0);
                    $(idcontent).append(newComer);
                    index += 1;
                }

            }, function (sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        );
    };

    this.BuildNewCommers = function (itemUrl, jobTitle, personName, department, filePath, isActive) {
        if (jobTitle == null) {
            jobTitle = "";
        }
        
        if (personName == null) {
            personName = "";
        }

        if (department == null) {
            department = "";
        }

        var imgurl = '';
        if (filePath != null && $(filePath).attr('src') != "") {
            imgurl = $(filePath).attr('src');
        }
        else {
            imgurl = '/teams/OurVoiceHN/Style Library/Images/default_image.gif';
        }

        var newComer = '';

        if (isActive) {
            newComer += ' <div class="active item">';
        } else {
            newComer += ' <div class="item">';
        }

        newComer += '<div class="newComerTitle">' + personName + '</div>';
        newComer += '<a href="' + itemUrl + '"><img class="pic-image img-responsive" src="' + imgurl + '" alt="' + personName + '"></a>';

        newComer += '<div class="newComerDepartment">';
        newComer += '<a href="' + itemUrl + '">';
        newComer += '<h1>' + jobTitle + '</h1>';
        newComer += '<p>' + department + '</p>';
        newComer += '</a>';
        newComer += '</div>';

        return newComer;
    };
    /*end javascript for New Comer*/
    /*=======================================*/
    function makeSlide() {
        $("#newcomer").hover(function () {
            $('#newcomer a.left').removeClass("hide");
            $('#newcomer a.right').removeClass("hide");
        }, function () {
            $('#newcomer a.left').addClass("hide");
            $('#newcomer a.right').addClass("hide");
        });
    }
}).call(this, jQuery);